<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { userLoginService } from '@/api/common.js'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
const form = ref()

// 登录表单数据
const loginFormModel = ref({
  username: '',
  password: ''
})

// 表单校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 20, message: '用户名必须是5~20位字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\w{4,32}$/,
      message: '密码必须是4~32位的字母、数字、下划线',
      trigger: 'blur'
    }
  ]
}

const userStore = useUserStore()
const router = useRouter()
const login = async () => {
  await form.value.validate()
  const res = await userLoginService(loginFormModel.value)
  if (res.data.data.role === '会员') {
    ElNotification({
      title: '登录失败',
      type: 'error'
    })
    return
  }
  userStore.setUser(res.data.data)
  userStore.setToken(res.data.data.token)
  ElNotification({
    title: '登录成功',
    type: 'success'
  })
  router.push('/')
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="18" class="bg"></el-col>
    <el-col :span="4" :offset="1" class="form">
      <el-form :model="loginFormModel" :rules="rules" ref="form" size="large" autocomplete="off">
        <el-form-item>
          <img class="logo" src="@/assets/logo.png" />
        </el-form-item>
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="loginFormModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginFormModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            @keyup.enter="login"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button @click="login" class="button" type="primary" auto-insert-space>登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background: url('@/assets/login_background.jpg') no-repeat center / cover;
    background-position-x: left;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .logo {
      width: 200px;
      height: 200px;
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
